# -*- coding: utf-8 -*-
"""
Created on Sun Mar 29 11:55:55 2020

@author: simsh
"""

import pandas as pd
FILE_IN = "500Samples.csv"
FILE_OUT = "testProcessed.csv"
df=pd.read_csv(FILE_IN)

BASELINE=-1

#identify best partition alg for each scenario

#print("coreCount,minTaskUtil,maxTaskUtil,numPeriods,expected_score,score_distro,slope,timeout,runtime,binSize,minSamples,alg,model,RSA,extraArea,results", file=f)

bestPartition=[]
improvement=[]
for k in range(len(df.index)):
    coreCount=df['coreCount'].iloc[k]
    minTaskUtil=df['minTaskUtil'].iloc[k]
    maxTaskUtil=df['maxTaskUtil'].iloc[k]
    numPeriods=df['numPeriods'].iloc[k]
    expected_score=df['expected_score'].iloc[k]
    score_distro = df['score_distro'].iloc[k]
    slope = df['slope'].iloc[k]
    model=df['model'].iloc[k]
    rsa=df['RSA'].iloc[k]
    
    sameScenario=df[(df['coreCount'] == coreCount) &
                    (df['minTaskUtil'] == minTaskUtil) &
                    (df['maxTaskUtil'] == maxTaskUtil) &
                    (df['numPeriods'] == numPeriods) &
                    (df['expected_score'] == expected_score) &
                    (df['score_distro'] == score_distro) &
                    (df['slope'] == slope) &
                    (df['model'] == model) ]
                     
    bestRSA=max(sameScenario['RSA'])
    #tempList=sameScenario['RSA'].tolist()
    #how to manage ties?
    #in the event of a tie, bestPartition should be the lowest-numbered algorithm
    bestAlg=min(sameScenario[(sameScenario['RSA']==bestRSA)]['alg'])
    if df['alg'].iloc[k]==bestAlg:
        bestPartition.append(True)
    else:
        bestPartition.append(False)
        
    baselineRSA=df[(df['coreCount'] == coreCount) &
                (df['minTaskUtil'] == minTaskUtil) &
                (df['maxTaskUtil'] == maxTaskUtil) &
                (df['numPeriods'] == numPeriods) &
                (df['expected_score'] == expected_score) &
                (df['score_distro'] == score_distro) &
                (df['slope'] == slope) &
                (df['alg'] == BASELINE) ]['RSA'].iloc[0]

    improvement.append(rsa/baselineRSA)
    

df.insert(14, 'bestPartition', bestPartition)
df.insert(14, 'improvement', improvement)

#add column: RSA/RSA_baseline

#save results
df.to_csv(FILE_OUT)